//
//  ViewController.m
//  DevIPad
//
//  Created by HuCongshi on 15/11/14.
//  Copyright © 2015年 HuCongshi. All rights reserved.
//

#import "ViewController.h"
#import "macro.h"
#import "TabbedPages.h"
@interface ViewController ()
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *userpassword;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initItems];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)initItems {
    
    CGRect rx = [UIScreen mainScreen].bounds;
    NSLogINFO(@"UIScreen bounds [%f,%f,%f,%f]",rx.origin.x,rx.origin.y,rx.size.width,rx.size.height);
    UIImageView *items = [[UIImageView alloc] initWithFrame:rx];
    UIImage *backgound = [UIImage imageNamed:@"BGP.png"];
    items.image = backgound;
    [self.view addSubview:items];
    
    UILabel *systemHead = [[UILabel alloc] initWithFrame:CGRectMake(rx.size.width/2-300, 60, 600, 50)];
    systemHead.text = @"中国移动云资源管理系统";
    systemHead.textAlignment = NSTextAlignmentCenter;
    systemHead.textColor = [UIColor whiteColor];
    systemHead.font = [UIFont systemFontOfSize:36];
    [self.view addSubview:systemHead];
    
    UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(rx.size.width/2-88,120,176,126)];
    logo.image = [UIImage imageNamed:@"logo.png"];
    [self.view addSubview:logo];
    
    UITextField *custom = [[UITextField alloc] initWithFrame:CGRectMake(rx.size.width/2-320, 250, 640, 50)];
    [self.view addSubview:custom];
    custom.font = [UIFont systemFontOfSize:32];
    custom.backgroundColor = [UIColor whiteColor];
    custom.delegate = self;
    custom.tag = 101;
    
    UILabel *customLabel = [[UILabel alloc] initWithFrame:CGRectMake(rx.size.width/2-400, 250, 80, 50)];
    customLabel.text = @"账户";
    customLabel.font = [UIFont systemFontOfSize:32];
    customLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:customLabel];
    
    UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(rx.size.width/2-320, 320, 640, 50)];
    [self.view addSubview:password];
    password.font = [UIFont systemFontOfSize:32];
    password.backgroundColor = [UIColor whiteColor];
    password.delegate = self;
    password.secureTextEntry = YES;
    password.tag = 102;
    
    UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(rx.size.width/2-400, 320, 80, 50)];
    passwordLabel.text = @"密码";
    passwordLabel.font = [UIFont systemFontOfSize:32];
    passwordLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:passwordLabel];

    
    UIButton *login = [[UIButton alloc] initWithFrame:CGRectMake(rx.size.width/2-320,390,640,50)];
    login.backgroundColor = [UIColor blueColor];
    [login setTitle:@"登陆" forState:UIControlStateNormal];
    login.titleLabel.textColor = [UIColor whiteColor];
    login.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:login];
    [login addTarget:self action:@selector(onLoginClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//////////////////////////////////////////
-(void)onLoginClicked:(id)sender{
    NSLogINFO(@"user name %@, password %@",self.username, self.userpassword);
    BOOL TEST = YES;
    if ((([self.username isEqualToString:@"hucongshi"] || [self.username isEqualToString:@"hucongshi "] || [self.username isEqualToString:@"Hucongshi"] || [self.username isEqualToString:@"Hucongshi"]) &&  [self.userpassword isEqualToString:@"password"]) || TEST) {
        NSLogINFO(@"Login Sueecss");
        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        TabbedPages *tp = [mainStoryboard instantiateViewControllerWithIdentifier:@"TabPageNew"];
        [self.navigationController pushViewController:tp animated:YES];
        //[self presentViewController:tp animated:YES completion:nil];
    } else {
        UIAlertController *alertContorller = [UIAlertController alertControllerWithTitle:@"警告" message:@"密码或用户名错误!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alertContorller addAction:cancelAction];
        [self presentViewController:alertContorller animated:YES completion:nil];
    }
}
#pragma mark -UITextField Delegate Method
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.tag == 101) {
        self.username = textField.text;
    } else if (textField.tag == 102) {
        self.userpassword = textField.text;
    }
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (textField.tag == 101) {
        self.username = textField.text;
    } else if (textField.tag == 102) {
        self.userpassword = textField.text;
    }
    return YES;
}
@end
